//! 大括号的语句

use crate::parser::node::NodeRoot;
use crate::{lexer::token::Token, parser::statement::Statement};
use std::ops::{Deref, DerefMut};

#[derive(Debug, Clone)]
pub struct BlockStatement {
    node: NodeRoot,
    token: Token,
    statements: Vec<Box<dyn Statement>>,
}

impl BlockStatement {
    pub fn new(token: Token, statements: Vec<Box<dyn Statement>>) -> Self {
        let mut statements_literal = String::new();
        for s in &statements {
            statements_literal.push_str(s.cast_to_node().get_literal());
            statements_literal.push_str(" ");
        }
        let token_literal = format!(
            "token: {}, statements: {}",
            token.literal_ref(),
            statements_literal
        );
        let node = NodeRoot::new(token_literal, "BlockStatement".to_string());

        Self {
            node,
            token,
            statements,
        }
    }

    pub fn cast_to_node(&self) -> &NodeRoot {
        &*self
    }

    pub fn statements(&self) -> &[Box<dyn Statement>] {
        &self.statements
    }
}

impl Deref for BlockStatement {
    type Target = NodeRoot;

    fn deref(&self) -> &Self::Target {
        &self.node
    }
}

impl DerefMut for BlockStatement {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.node
    }
}
